

import java.io.FileOutputStream;
import java.util.*;
import java.io.IOException;

public class RunMe {
	
	private static Query userQuery;
	private static ExpandQuery queryExpander;
	private static int precision;
	private static StopWord sw;
	private static FileOutputStream oos;
	
	
	public static void main(String[] args) {
		
		try{  oos=new FileOutputStream("Transcript.txt",true);}catch(Exception e){}
		System.out.println("Please enter query divided by space (e.g \"the actor rock\")");
		Scanner in = new Scanner(System.in);
		userQuery = new Query(in.nextLine());
		 sw = new StopWord();
		boolean correct = false;
		System.out.println("please enter your desired precision: (a number between 0 - 10)");
		
		while(!correct)
		{
		try{
		precision=Integer.parseInt(in.nextLine());
		correct=true;
		if(precision<0||precision>10)
		{
			System.err.println("the number should be between 0 - 10, please try again:");
			
			correct=false;
		}
		
	       }catch(NumberFormatException e){System.err.println("cannot understand your input, should be a number between 0 - 10");correct=false;System.out.println("try again:");}
		}
		System.out.println("The desired presicion is: "+precision+"@10");
		try{oos.write(("The desired presicion is: "+precision+"@10 \n").getBytes());}catch(Exception e){}
	       process(userQuery,in);
	    }
	
	public static void process(Query userQuery, Scanner in)
		{
		try{oos.write("    -------------------------------------\n".getBytes());
		    oos.write("    -------------------------------------\n".getBytes());
			oos.write(("ROUND "+(userQuery.getRound()+1)+"\n").getBytes());}catch(Exception e){}
		Presult rs = userQuery.runquery();
		List list = new ArrayList();
		List list2 = new ArrayList();
		List num = new ArrayList();
		
		System.out.println("Please enter the index number of the relevant result, divide them using space: (like this: \"1 3 5 7\", you will choose 1,3,5,7 results), if there is no relevant result, please choose 0");

		boolean correct=false;
		while(!correct)
		{
		try{	
		StringTokenizer st = new StringTokenizer(in.nextLine());
		int count = st.countTokens();
		oos.write("\n".getBytes());
		oos.write("The relevant result index are :".getBytes());
		while(st.hasMoreTokens())
		{   String s = st.nextToken();
		    if(s.equals("0"))
		    {
		    	System.out.println("You choose 0, means that there is no relevant results, PRECISION 0, program terminate");
		    	oos.write("You choose 0, means that there is no relevant results, program terminate".getBytes());
		    	oos.close();
		    	System.exit(0);
		    }
			list.add(rs.get(Integer.parseInt(s)-1));
			num.add(new Integer(Integer.parseInt(s)-1));
			
			oos.write(("Result "+s+" ").getBytes());
		}
		oos.write("\n".getBytes());
		if(count==10){System.out.println("The precision is 1.0, program terminate");
		oos.write(("PRECISION 1.0 \n").getBytes());oos.close();System.exit(0);}
		oos.write(("PRECISION 0."+count+"\n").getBytes());
		oos.write("\n".getBytes());
		correct=true;
		
		if(count>=precision)
		{
			System.out.println("You have already reached your desired precision, which is :"+precision+"@10, program terminate");
			oos.write(("You have already reached your desired precision, which is 0."+count).getBytes());
			oos.close();
			System.exit(0);
		}
		}catch(NumberFormatException e){System.err.println("cannot understand your input, you should only input numbers and space, like \"1 3 5 7\", if there is no relevant result, please choose 0");correct=false;System.out.println("try again:");}
		catch(IOException e){}
		}
		int[] c = new int[num.size()];
		int j=0;
		for(int i=0;i<num.size();i++)
		{
			c[j++]=((Integer)num.get(i)).intValue();
			
		}
        Presult relevantset = new Presult(list);
        
        for(j=0; j<10;j++)
        {   boolean b = true;
        	for(int k=0;k<num.size();k++)
        	{
        		if(j==c[k]) {b=false;break;}
        		
        	}
        	if(b)
        	{   //System.out.println(j);
        		list2.add(rs.get(j));
        	}
        	
        }
       
        Presult nonrelevantset = new Presult(list2);
		
        
        queryExpander = new ExpandQuery(sw);
        process(queryExpander.apply(userQuery, relevantset, nonrelevantset),in);
        		
	}

}
